﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;

namespace BDSim
{


    #region 位置、坐标类型等枚举类型定义
    /// <summary>
    /// 位置、速度单位
    /// </summary>
    public enum VelPosUnit
    {
        Km,
        m
    };

    /// <summary>
    /// 坐标系统
    /// </summary>
    public enum CoordinateSystem
    {
        J2000,
        CGCS2000,
        WGS84,
    }

    /// <summary>
    /// 位置信息
    /// </summary>
    public enum BLHXYZEnum
    {
        BLH,    //  纬经高
        XYZ     //  笛卡尔坐标
    }

    #endregion

    #region 位置以及定位结果类
    /// <summary>
    /// 用定位信息
    /// </summary>
    public struct UserPPositionData
    {
        /// <summary>
        /// 历元
        /// </summary>
        public UTCTime Time;

        /// <summary>
        /// 准确位置信息
        /// </summary>
        public NodePosVel Original_XYZ;

        /// <summary>
        /// 定位结果
        /// </summary>
        public NodePosVel PPosition_XYZ;

        /// <summary>
        /// 定位误差
        /// </summary>
        public NodePosVel Error_XYZ;
    }

    /// <summary>
    /// 用于存放位置和位置误差的类
    /// </summary>
    public class PosPosErrClass
    {
        /// <summary>
        /// 历元
        /// </summary>
        public UTCTime UTC;

        /// <summary>
        /// 该历元下的位置信息
        /// </summary>
        public double[] Pos = new double[3];

        /// <summary>
        /// 该历元下的误差
        /// </summary>
        public double[] PosErr = new double[3];

        /// <summary>
        /// 属性初始化
        /// </summary>
        public PosPosErrClass()
        {
            UTC = new UTCTime();
            Pos = new double[3];
            PosErr = new double[3];
        }

        /// <summary>
        /// 属性初始化
        /// </summary>
        /// <param name="_UTCTime">历元时间</param>
        /// <param name="_BLH">纬经高</param>
        /// <param name="_ErrBLH">纬经高误差</param>
        public PosPosErrClass(UTCTime _UTCTime, BLH_PosVel _BLH, BLH_PosVel _ErrBLH)
        {
            UTC = _UTCTime;
            Pos[0] = _BLH.B;
            Pos[1] = _BLH.L;
            Pos[2] = _BLH.H;

            PosErr[0] = _ErrBLH.B;
            PosErr[1] = _ErrBLH.L;
            PosErr[2] = _ErrBLH.H;
        }

        /// <summary>
        /// 属性初始化
        /// </summary>
        /// <param name="_UTCTime">历元时间</param>
        /// <param name="_XYZ">位置</param>
        /// <param name="_ErrXYZ">位置误差</param>
        public PosPosErrClass(UTCTime _UTCTime, NodePosVel _XYZ, NodePosVel _ErrXYZ)
        {
            UTC = _UTCTime;
            Pos[0] = _XYZ.X;
            Pos[1] = _XYZ.Y;
            Pos[2] = _XYZ.Z;

            PosErr[0] = _ErrXYZ.X;
            PosErr[1] = _ErrXYZ.Y;
            PosErr[2] = _ErrXYZ.Z;
        }
    }

    /// <summary>
    /// 用户跟踪站类
    /// </summary>
    public class UserTrajectoryClass
    {
        /// <summary>
        /// 纬经高
        /// </summary>
        public BLH_PosVel UserPosBLH;

        /// <summary>
        /// 位置
        /// </summary>
        public NodePosVel UserPosXYZ;

        /// <summary>
        /// 纬经高和位置
        /// </summary>
        //public double[] Pos = new double[6];

        /// <summary>
        /// 属性初始化
        /// </summary>
        public UserTrajectoryClass()
        {
            UserPosBLH = new BLH_PosVel();
            UserPosXYZ = new NodePosVel();
        }

        public UserTrajectoryClass(BLH_PosVel _BLH, NodePosVel _XYZ)
        {
            UserPosBLH = _BLH;
            UserPosXYZ = _XYZ;
        }
    }

    #endregion

    #region 各类数据写文件和转字符串类
    public class OrbitDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 前言
        /// </summary>
        string Introduction = null;

        /// <summary>
        /// 卫星名称
        /// </summary>
        private string SatName = null;

        /// <summary>
        /// 数据说明（km)
        /// </summary>
        private const string HeaderKm = "        Time (UTC)               x (km)                y (km)              z (km)           vx (km/sec)       vy (km/sec)       vz (km/sec)";

        /// <summary>
        /// 数据说明(m)
        /// </summary>
        private const string Headerm = "        Time (UTC)               x (m)                 y (m)               z (m)            vx (m/sec)        vy (m/sec)        vz (m/sec)";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------    -----------------    -----------------    -----------------    --------------    --------------    --------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;


        /// <summary>
        /// 轨道数据显示缓存
        /// </summary>
        List<string> DataBuffer = new List<string>();
        #endregion

        #region  文件保存接口函数
        /// <summary>
        /// 写文件初始化
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_SatName">卫星名</param>
        /// <param name="coordinateSystem">坐标系统</param>
        /// <param name="unit">单位</param>
        /// <param name="_Declare">声明</param>
        /// <returns></returns>
        public bool InitSatWriteFileCfg(string _FileName, string _SatName, CoordinateSystem coordinateSystem = CoordinateSystem.J2000, VelPosUnit unit = VelPosUnit.m, string _Declare = "北斗开放实验室")
        {
            FileName = _FileName;
            Declare = _Declare;
            SatName = _SatName;
            string Header = null;

            switch (unit)
            {
                case VelPosUnit.Km:
                    Header = HeaderKm;
                    break;
                case VelPosUnit.m:
                    Header = Headerm;
                    break;
            }
            Introduction = "Satellite-" + SatName + ": " + coordinateSystem.ToString() + " 位置 & 速度";

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();
                WriteFileState = true;
            }
            catch (Exception)
            {
                WriteFileState = false;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 直接根据历元，位置坐标信息写文件
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="Z"></param>
        /// <param name="Vx"></param>
        /// <param name="Vy"></param>
        /// <param name="Vz"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, double X, double Y, double Z, double Vx, double Vy, double Vz)
        {
            if (WriteFileState)
            {
                try
                {
                    UTCToStringClass utcstr = new UTCToStringClass();
                    string UTCStr = utcstr.NewUTCToString(UTC);
                    string content = UTCStr + "    " + X.ToString("0.000000").PadLeft(17, ' ') +
                        "    " + Y.ToString("0.000000").PadLeft(17, ' ') +
                        "    " + Z.ToString("0.000000").PadLeft(17, ' ') +
                        "    " + Vx.ToString("0.000000").PadLeft(14, ' ') +
                        "    " + Vy.ToString("0.000000").PadLeft(14, ' ') +
                        "    " + Vz.ToString("0.000000").PadLeft(14, ' ') + "\r\n";

                    File.AppendAllText(FileName, content);
                }
                catch (Exception)
                {
                    WriteFileState = false;
                    return false;
                }

            }
            return true;
        }

        /// <summary>
        /// 根据卫星轨道位置速度信息写文件
        /// </summary>
        /// <param name="NodePosVel">卫星轨道位置速度信息</param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(NodePosVel NodePosVel)
        {
            if (WriteFileState)
            {
                WriteFile(NodePosVel.Epoch, NodePosVel.x, NodePosVel.y, NodePosVel.z, NodePosVel.vx,
                    NodePosVel.vy, NodePosVel.vz);
                return true;
            }
            else
            {
                return false;
            }

        }
        #endregion

        #region///字符输出接口函数

        /// <summary>
        /// 轨道信息转字符串初始化
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_SatName">卫星名</param>
        /// <param name="coordinateSystem">坐标系统</param>
        /// <param name="unit">单位</param>
        /// <param name="_Declare">声明</param>
        /// <returns></returns>
        public void InitWriteString(string _SatName, CoordinateSystem coordinateSystem = CoordinateSystem.J2000, VelPosUnit unit = VelPosUnit.m, string _Declare = "北斗开放实验室")
        {
            Declare = _Declare;
            SatName = _SatName;
            Introduction = "Satellite-" + SatName + ": " + coordinateSystem.ToString() + " 位置 & 速度";
            WriteFileState = true;
        }

        /// <summary>
        /// 直接根据历元，位置坐标信息转字符串
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="Z"></param>
        /// <param name="Vx"></param>
        /// <param name="Vy"></param>
        /// <param name="Vz"></param>
        /// <returns>转换结果</returns>
        public string WriteString(UTCTime UTC, double X, double Y, double Z, double Vx, double Vy, double Vz)
        {
            string content = "";
            if (WriteFileState)
            {
                try
                {
                    UTCToStringClass utcstr = new UTCToStringClass();
                    string UTCStr = utcstr.NewUTCToString(UTC);
                    content = UTCStr + "    " + X.ToString("0.000000").PadLeft(17, ' ') +
                        "    " + Y.ToString("0.000000").PadLeft(17, ' ') +
                        "    " + Z.ToString("0.000000").PadLeft(17, ' ') +
                        "    " + Vx.ToString("0.000000").PadLeft(14, ' ') +
                        "    " + Vy.ToString("0.000000").PadLeft(14, ' ') +
                        "    " + Vz.ToString("0.000000").PadLeft(14, ' ');
                }
                catch (Exception)
                {
                    WriteFileState = false;
                }

            }
            return content;
        }

        /// 根据卫星轨道位置速度信息转字符串
        /// </summary>
        /// <param name="NodePosVel">卫星轨道位置速度信息</param>
        /// <returns>转换结果</returns>
        public string WriteString(NodePosVel NodePosVel)
        {
            string content = "";
            if (WriteFileState)
            {
                content = WriteString(NodePosVel.Epoch, NodePosVel.x, NodePosVel.y, NodePosVel.z, NodePosVel.vx,
                    NodePosVel.vy, NodePosVel.vz);
                return content;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 把轨道数据写成字符串集合List<string>
        /// </summary>
        /// <param name="orbitDatas">轨道数据集合</param>
        /// <returns>转换结果</returns>
        public List<string> WriteToStringList(List<NodePosVel> orbitDatas)
        {
            DataBuffer.Add(Declare);
            DataBuffer.Add("");
            DataBuffer.Add(Introduction);
            DataBuffer.Add("");
            DataBuffer.Add(Headerm);
            DataBuffer.Add(SplitLine);
            foreach (var item in orbitDatas)
            {
                DataBuffer.Add(WriteString(item));
            }
            return DataBuffer;
        }
        #endregion
    }


    /// <summary>
    /// 钟差数据，保存时间和钟差值
    /// </summary>
    public class ClockDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 前言
        /// </summary>
        string Introduction = null;
        /// <summary>
        /// 对象名称
        /// </summary>
        private string NodeName = null;

        /// <summary>
        /// 数据说明（s)
        /// </summary>
        private const string Header = "       Time (UTC)                 ClkErr(s)    ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------    --------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        /// <summary>
        /// 钟差数据转字符串缓存
        /// </summary>
        List<string> DataBuffer = new List<string>();
        #endregion

        #region 接口定义
        /// <summary>
        /// 写钟差文件初始化
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_NodeName">节点名</param>
        /// <param name="NodeType">节点类型</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitClockDataFile(string _FileName, string _NodeName, string _Declare = "北斗开放实验室")
        {

            FileName = _FileName;
            NodeName = _NodeName;
            Declare = _Declare;

            //string Introduction = NodeType.ToString() + "-" + NodeName + ":钟差数据";
            string Introduction = NodeName + ":钟差数据";
            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();
            }
            catch (Exception)
            {
                WriteFileState = false;
                return false;
            }

            WriteFileState = true;
            return true;
        }


        /// <summary>
        /// 单个钟差数据写文件
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="ClkErr">钟差</param>
        /// <returns>True 成功； False 失败</returns>        
        public bool WriteFile(UTCTime UTC, double ClkErr)
        {
            if (WriteFileState)
            {
                try
                {
                    UTCToStringClass utcstr = new UTCToStringClass();
                    string content = utcstr.NewUTCToString(UTC) + "    " + ClkErr.ToString().PadLeft(20, ' ') + "\r\n";
                    File.AppendAllText(FileName, content);
                }
                catch (Exception)
                {
                    WriteFileState = false;
                }
            }
            return true;
        }

        /// <summary>
        /// 钟差数据集合写文件
        /// </summary>
        /// <param name="ClkDataList">钟差数据集合</param>
        /// <returns>True 成功； False 失败</returns>  
        public bool WriteFile(List<ClkSimData> ClkDataList)
        {
            if (WriteFileState)
            {
                foreach (var item in ClkDataList)
                {
                    WriteFile(item.SimTime, item.ClkError);
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region//返回字符串接口
        /// <summary>
        /// 钟差转字符串初始化
        /// </summary>
        /// <param name="_NodeName">节点名</param>
        /// <param name="NodeType">节点类型</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public void InitClockDataFile(string _NodeName, string _Declare = "北斗开放实验室")
        {

            NodeName = _NodeName;
            Declare = _Declare;
            //Introduction = NodeType.ToString() + "-" + NodeName + "-钟差数据";
            Introduction = NodeName + "：钟差数据";
            WriteFileState = true;
        }

        /// <summary>
        /// 单个钟差数据转字符串
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="ClkErr">钟差</param>
        /// <returns>True 成功； False 失败</returns>  
        public string WriteString(UTCTime UTC, double ClkErr)
        {
            string content = null;
            if (WriteFileState)
            {
                UTCToStringClass utcstr = new UTCToStringClass();
                content = utcstr.NewUTCToString(UTC) + "    " + ClkErr.ToString().PadLeft(20, ' ');
            }
            return content;
        }

        /// <summary>
        /// 钟差数据集合转字符串集合
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="ClkErr">钟差</param>
        /// <returns>True 成功； False 失败</returns>  
        public List<string> WriteToStringList(List<ClkSimData> dataList)
        {
            DataBuffer.Add(Declare);
            DataBuffer.Add("");
            DataBuffer.Add(Introduction);
            DataBuffer.Add("");
            DataBuffer.Add(Header);
            DataBuffer.Add(SplitLine);
            for (int i = 0; i < dataList.Count; i++)
            {
                DataBuffer.Add(WriteString(dataList[i].SimTime, dataList[i].ClkError));
            }
            return DataBuffer;
        }
        #endregion
    }

    /// <summary>
    /// 电离层写文件
    /// </summary>
    public class EightIonoSphereWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 电离层八参数
        /// </summary>
        private const string Header = "       Time (UTC)               Alpha0                 Alpha1                 Alpha2                 Alpha3                  Beta0                  Beta1                  Beta2                  Beta3  ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------  ---------------------  ---------------------  ---------------------  ---------------------  ---------------------  ---------------------  ---------------------  ---------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        #endregion

        #region 接口定义
        /// <summary>
        /// 电离层八参写文件初始化
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitEightIonoSphereDataFile(string _FileName, string _Declare = "北斗开放实验室")
        {
            FileName = _FileName;
            Declare = _Declare;

            string Introduction = "电离层八参数";

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();

                WriteFileState = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                WriteFileState = false;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 单个电离层八参写文件
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="Alpha0">对应电离层八个参数</param>
        /// <param name="Alpha1"></param>
        /// <param name="Alpha2"></param>
        /// <param name="Alpha3"></param>
        /// <param name="Beta0"></param>
        /// <param name="Beta1"></param>
        /// <param name="Beta2"></param>
        /// <param name="Beta3"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, double Alpha0, double Alpha1, double Alpha2, double Alpha3, double Beta0, double Beta1, double Beta2, double Beta3)
        {
            if (WriteFileState)
            {
                UTCToStringClass utcstr = new UTCToStringClass();
                string Content = utcstr.NewUTCToString(UTC);
                Content += "  " + Alpha0.ToString().PadLeft(21, ' ');
                Content += "  " + Alpha1.ToString().PadLeft(21, ' ');
                Content += "  " + Alpha2.ToString().PadLeft(21, ' ');
                Content += "  " + Alpha3.ToString().PadLeft(21, ' ');
                Content += "  " + Beta0.ToString().PadLeft(21, ' ');
                Content += "  " + Beta1.ToString().PadLeft(21, ' ');
                Content += "  " + Beta2.ToString().PadLeft(21, ' ');
                Content += "  " + Beta3.ToString().PadLeft(21, ' ') + "\r\n";

                try
                {
                    File.AppendAllText(FileName, Content);
                }
                catch (Exception)
                {
                    WriteFileState = false;
                    //throw ex;
                }
            }
            return true;
        }
        #endregion

    }

    /// <summary>
    /// 星历16参写文件
    /// </summary>
    public class SixTeen_EphemerisDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 对象名称
        /// </summary>
        private string SatName = null;

        /// <summary>
        /// 数据说明
        /// </summary>
        private const string Header = "       Time (UTC)            toe(s)           sqrtA(m½)                e                   i(rad)              Omega0(rad)             w(rad)                M(rad)             deltaN(rad/s)        OmegaDot(rad/s)         idot(rad/s)           Crs(m)            Crc(m)             Cus(Rad)              Cuc(Rad)             Cis(Rad)               Cic(Rad)     ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------    ----------    ------------------    ------------------    ------------------    ------------------    ------------------    ------------------    ------------------    ------------------    ------------------    --------------    --------------    ------------------    ------------------    ------------------    ------------------";

        /// <summary>
        /// 前言
        /// </summary>
        string Introduction = null;
        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        /// <summary>
        /// 星历16参转字符串缓存
        /// </summary>
        List<string> DataBuffer = new List<string>();

        #endregion

        #region 接口定义
        /// <summary>
        /// 初始化写星历十六参文件
        /// </summary>
        /// <param name="_FileName">文件名</param>
        /// <param name="_SatName">卫星名</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitSixTeen_EphemerisData(string _FileName, string _SatName, string _Declare = "北斗开放实验室")
        {
            FileName = _FileName;
            SatName = _SatName;
            Declare = _Declare;

            Introduction = "Satellite-" + SatName + ": 星历16参数";

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();

                WriteFileState = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                WriteFileState = false;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 星历十六参写文件
        /// </summary>
        /// <param name="SixTeen_EphemerisParam">星历十六参</param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(Ephemeris_16 SixTeen_EphemerisParam)
        {
            if (WriteFileState)
            {
                try
                {
                    UTCToStringClass utcstr = new UTCToStringClass();
                    string UTCStr = utcstr.NewUTCToString(SixTeen_EphemerisParam.t);

                    string content = UTCStr
                         + "    " + SixTeen_EphemerisParam.toe.ToString("F3").PadLeft(10, ' ')
                         + "    " + SixTeen_EphemerisParam.sqrtA.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.e.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.i.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Omega0.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.w.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.M.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.deltaN.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.OmegaDot.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.idot.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Crs.ToString("F4").PadLeft(14, ' ')
                         + "    " + SixTeen_EphemerisParam.Crc.ToString("F4").PadLeft(14, ' ')
                         + "    " + SixTeen_EphemerisParam.Cus.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cuc.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cis.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cic.ToString("E10").PadLeft(18, ' ') + "\r\n";
                    File.AppendAllText(FileName, content);
                }
                catch (Exception)
                {
                    WriteFileState = false;
                    //throw ex;
                }
            }
            return true;
        }
        #endregion

        #region//返回字符串接口
        /// <summary>
        /// 初始化星历16参转字符串初始化
        /// </summary>
        /// <param name="_SatName">卫星名</param>
        /// <param name="NodeType">节点类型</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitSixTeen_EphemerisData(string _SatName, string _Declare = "北斗开放实验室")
        {
            SatName = _SatName;
            Declare = _Declare;
            //Introduction = NodeType.ToString() + "-" + SatName + ": 星历16参数";
            Introduction = SatName + ": 星历16参数";
            WriteFileState = true;
            return true;
        }

        /// <summary>
        /// 星历16参转字符串
        /// </summary>
        /// <param name="SixTeen_EphemerisParam">星历16参</param>
        /// <returns>True 成功； False 失败</returns>
        public string WriteString(Ephemeris_16 SixTeen_EphemerisParam, EConstellationType satType)
        {
            UTCToStringClass utcstr = new UTCToStringClass();
            string UTCStr = utcstr.NewUTCToString(SixTeen_EphemerisParam.t);

            double jd = 0.0;
            int wn = 0;
            double toe = 0;
            switch (satType)
            {
                case EConstellationType.BD:
                    TimeSpaceDll.UTC2BDT(SixTeen_EphemerisParam.t.JD, out jd);
                    TimeSpaceDll.BDT_JD2WnSec(jd, out wn, out toe);
                    break;
                case EConstellationType.GPS:
                    TimeSpaceDll.UTC2GPST(SixTeen_EphemerisParam.t.JD, out jd);
                    TimeSpaceDll.GPST_JD2WnSec(jd, out wn, out toe);
                    break;
                case EConstellationType.GLONASS:
                    TimeSpaceDll.UTC2GPST(SixTeen_EphemerisParam.t.JD, out jd);
                    TimeSpaceDll.GPST_JD2WnSec(jd, out wn, out toe);
                    break;
                case EConstellationType.GALILEO:
                    TimeSpaceDll.UTC2GST(SixTeen_EphemerisParam.t.JD, out jd);
                    TimeSpaceDll.GST_JD2WnSec(jd, out wn, out toe);
                    break;
                default:
                    break;
            }

            string content = UTCStr
                         + "    " + wn.ToString("F3").PadLeft(10, ' ')
                         + "    " + toe.ToString("F3").PadLeft(10, ' ')
                         + "    " + SixTeen_EphemerisParam.sqrtA.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.e.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.i.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Omega0.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.w.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.M.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.deltaN.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.OmegaDot.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.idot.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Crs.ToString("F4").PadLeft(14, ' ')
                         + "    " + SixTeen_EphemerisParam.Crc.ToString("F4").PadLeft(14, ' ')
                         + "    " + SixTeen_EphemerisParam.Cus.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cuc.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cis.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cic.ToString("E10").PadLeft(18, ' ') + "\r\n";
            return content;
        }

        /// <summary>
        /// 星历16参转字符串
        /// </summary>
        /// <param name="SixTeen_EphemerisParam">星历16参</param>
        /// <returns>True 成功； False 失败</returns>
        public string WriteString(Ephemeris_16 SixTeen_EphemerisParam)
        {
            UTCToStringClass utcstr = new UTCToStringClass();
            string UTCStr = utcstr.NewUTCToString(SixTeen_EphemerisParam.t);

            string content = UTCStr
                         + "    " + SixTeen_EphemerisParam.toe.ToString("F3").PadLeft(10, ' ')
                         + "    " + SixTeen_EphemerisParam.sqrtA.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.e.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.i.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Omega0.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.w.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.M.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.deltaN.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.OmegaDot.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.idot.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Crs.ToString("F4").PadLeft(14, ' ')
                         + "    " + SixTeen_EphemerisParam.Crc.ToString("F4").PadLeft(14, ' ')
                         + "    " + SixTeen_EphemerisParam.Cus.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cuc.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cis.ToString("E10").PadLeft(18, ' ')
                         + "    " + SixTeen_EphemerisParam.Cic.ToString("E10").PadLeft(18, ' ') + "\r\n";
            return content;
        }

        /// <summary>
        /// 星历16参集合转字符串集合
        /// </summary>
        /// <param name="dataList">星历16参集合</param>
        /// <returns>转换结果</returns>
        public List<string> WriteToStringList(List<Ephemeris_16> dataList)
        {
            DataBuffer.Add(Declare);
            DataBuffer.Add("");
            DataBuffer.Add(Introduction);
            DataBuffer.Add("");
            DataBuffer.Add(Header);
            DataBuffer.Add(SplitLine);

            foreach (var item in dataList)
            {
                DataBuffer.Add(WriteString(item));
            }
            return DataBuffer;
        }

        /// <summary>
        /// 星历16参集合转字符串集合
        /// </summary>
        /// <param name="dataList">星历16参集合</param>
        /// <returns>转换结果</returns>
        public List<string> WriteToStringList(List<Ephemeris_16> dataList, EConstellationType satType)
        {
            DataBuffer.Add(Declare);
            DataBuffer.Add("");
            DataBuffer.Add(Introduction);
            DataBuffer.Add("");
            DataBuffer.Add(Header);
            DataBuffer.Add(SplitLine);

            foreach (var item in dataList)
            {
                DataBuffer.Add(WriteString(item, satType));
            }
            return DataBuffer;
        }

        #endregion
    }

    /// <summary>
    /// 观测数据写文件
    /// </summary>
    public class MeasureResultDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 前言
        /// </summary>
        private string Introduction = null;

        /// <summary>
        /// 分割符
        /// </summary>
        private string SplitLine = null;

        /// <summary>
        /// 数据说明列
        /// </summary>
        private string Header = null;

        /// <summary>
        /// 频率信息
        /// </summary>
        private string FreqInfo = null;

        /// <summary>
        /// 源节点名称
        /// </summary>
        private string SrcName = null;

        /// <summary>
        /// 目标节点名称
        /// </summary>
        //private string DestName = null;

        ///// <summary>
        ///// 源节点类型
        ///// </summary>
        //private ModelType localNodeType;

        ///// <summary>
        ///// 目标节点类型
        ///// </summary>
        //private ModelType remoteNodeType;

        /// <summary>
        /// 数据说明列(km)
        /// </summary
        private const string OneFreqHeaderKm = "       Time (UTC)               测距值(km)             载波相位(m)              多普勒频移(Hz)           伪距变率(km/s)  ";

        /// <summary>
        /// 数据说明列(m)
        /// </summary>
        private const string OneFreqHeaderm = "       Time (UTC)               测距值(m)              载波相位(m)              多普勒频移(Hz)           伪距变率(m/s)   ";

        /// <summary>
        /// 数据说明列(m)
        /// </summary>
        private const string UpDownObsHeaderm = "       Time (UTC)             周计数           周内秒           发送ID        接收ID              测距值(m)              载波相位(m)              多普勒频移(Hz)           伪距变率(m/s)   ";
        /// <summary>
        /// 分隔符
        /// </summary>
        private const string UpDownObsSplitLine = "-----------------------    ------------    ---------------    ----------    ----------    -------------------    ----------------------    ----------------------    -------------------";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string OneFreqSplitLine = "-----------------------    -------------------    ----------------------    ----------------------    -------------------";

        /// <summary>
        /// 双频点数据说明列(km)
        /// </summary>
        private const string TwoFreqHeaderKm = "       Time (UTC)              测距值1(km)            载波相位1(m)              多普勒频移1(Hz)           伪距变率1(km/s)         测距值2(km)           载波相位2(m)             多普勒频移2(Hz)          伪距变率2(km/s)  ";

        /// <summary>
        /// 双频点数据说明列(m)
        /// </summary>
        private const string TwoFreqHeaderm = "       Time (UTC)              测距值1(m)             载波相位1(m)              多普勒频移1(Hz)           伪距变率1(m/s)          测距值2(m)            载波相位2(m)             多普勒频移2(Hz)          伪距变率2(m/s)   ";

        /// <summary>
        /// 双频点数据分割符
        /// </summary>
        private const string TwoFreqSplitLine = "-----------------------    -------------------    ----------------------    ----------------------    -------------------    -------------------    ----------------------    ----------------------    -------------------";

        /// <summary>
        /// 三频点数据说明列(km)
        /// </summary>
        private const string ThreeFreqHeaderKm = "       Time (UTC)               测距值1(km)            载波相位1(m)             多普勒频移1(Hz)         伪距变率1(km/s)           测距值2(km)            载波相位2(m)            多普勒频移2(Hz)          伪距变率2(km/s)         测距值3(km)              载波相位3(m)           多普勒频移3(Hz)           伪距变率3(km/s)  ";

        /// <summary>
        /// 三频点数据说明列(m)
        /// </summary>
        private const string ThreeFreqHeaderm = "       Time (UTC)           卫星编号         测距值1(m)             载波相位1(m)             多普勒频移1(Hz)          伪距变率1(m/s)           测距值2(m)             载波相位2(m)            多普勒频移2(Hz)          伪距变率2(m/s)           测距值3(m)              载波相位3(m)            多普勒频移3(Hz)           伪距变率3(m/s)  ";

        /// <summary>
        /// 三频点数据分割符
        /// </summary>
        private const string ThreeFreqSplitLine = "-----------------------    ----------    -------------------    ----------------------    ----------------------    -------------------    -------------------    ----------------------    ----------------------    -------------------    -------------------    ----------------------    ----------------------    -------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        /// <summary>
        /// 观测数据转字符串缓存
        /// </summary>
        List<string> DataBuffer = new List<string>();
        #endregion

        #region 写文件接口定义
        /// <summary>
        /// 初始化观测数据写文件
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_SrcName">源节点名</param>
        /// <param name="FreqNum">频点个数，不大于三，从1开始</param>
        /// <param name="Freq1">频点1</param>
        /// <param name="Freq2">频点2</param>
        /// <param name="Freq3">频点3</param>
        /// <param name="_localNodeType">源节点类型</param>
        /// <param name="_DestName">目标节点名</param>
        /// <param name="_remoteNodeType">目标节点类型</param>
        /// <param name="_Declare">声明</param>
        /// <param name="unit">单位</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitCrossLinkDataFile(string _FileName, string _SrcName, int FreqNum = 1, double Freq1 = 0.0, double Freq2 = 0.0, double Freq3 = 0.0, string _Declare = "北斗开放实验室", VelPosUnit unit = VelPosUnit.m)
        {
            switch (FreqNum)
            {
                case 1:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = OneFreqHeaderKm;
                            break;
                        case VelPosUnit.m:
                            Header = OneFreqHeaderm;
                            break;
                    }
                    SplitLine = OneFreqSplitLine;
                    break;
                case 2:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = TwoFreqHeaderKm;
                            break;
                        case VelPosUnit.m:
                            Header = TwoFreqHeaderm;
                            break;
                    }
                    SplitLine = TwoFreqSplitLine;
                    break;
                case 3:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = ThreeFreqHeaderKm;
                            break;
                        case VelPosUnit.m:
                            Header = ThreeFreqHeaderm;
                            break;
                    }
                    SplitLine = ThreeFreqSplitLine;
                    break;
                default:
                    return false;
            }

            for (int i = 1; i <= FreqNum; i++)
            {
                switch (i)
                {
                    case 1:
                        FreqInfo += "   Freq" + i + "=" + Freq1.ToString() + "Mhz\t";
                        break;
                    case 2:
                        FreqInfo += "   Freq" + i + "=" + Freq2.ToString() + "Mhz\t";
                        break;
                    case 3:
                        FreqInfo += "   Freq" + i + "=" + Freq3.ToString() + "Mhz\t";
                        break;
                    default:

                        break;
                }
            }

            FileName = _FileName;
            SrcName = _SrcName;
            //localNodeType = _localNodeType;
            Declare = _Declare;

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(SrcName + "观测数据" + "\t\t\t\t" + FreqInfo + "\r\n");
                sw.Close();

                WriteFileState = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                WriteFileState = false;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 单个观测数据写文件
        /// </summary>
        /// <param name="measure">观测数据</param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(MeasureResult measure)
        {
            if (WriteFileState)
            {
                try
                {
                    bool IsNotAllZero = false;
                    string content = null;
                    if (measure.pseudo.Length == measure.DotPseudo.Length && measure.pseudo.Length == measure.DoplShift.Length)
                    {
                        int WNBitWidth = 0;
                        if (measure.WN > 0)
                        {
                            WNBitWidth = Convert.ToInt32(Math.Log10(measure.WN) + 1);
                        }
                        content = measure.WN.ToString().PadLeft(WNBitWidth + (6 - WNBitWidth) / 2, ' ').PadRight(6, ' ') + "  " + string.Format("{0,10:F1}", measure.SOW);

                        for (int i = 0; i < measure.pseudo.Length; i++)
                        {
                            if (measure.pseudo[i] != 0 || measure.Carri[i] != 0 || measure.DoplShift[i] != 0 || measure.DotPseudo[i] != 0)
                            {
                                IsNotAllZero = true;
                            }
                            content += "    " + measure.pseudo[i].ToString().PadLeft(19, ' ') + "    " +
                                measure.Carri[i].ToString().PadLeft(22, ' ') + "    " + measure.DoplShift[i].ToString().PadLeft(22, ' ') + "    " +
                                measure.DotPseudo[i].ToString().PadLeft(19, ' ');
                        }
                        content += "\r\n";
                    }
                    else
                    {
                        WriteFileState = false;
                        return false;
                    }

                    if (IsNotAllZero)
                    {
                        File.AppendAllText(FileName, content);
                    }

                }
                catch (Exception)
                {
                    WriteFileState = false;
                    //throw ex;
                }
            }
            return true;
        }

        /// <summary>
        /// 观测数据集合写文件，每种观测数据存在一个List中
        /// </summary>
        /// <param name="ListMeasure">观测数据集合</param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(List<List<MeasureResult>> ListMeasure, Dictionary<int, string> SatIDToSatNameDic)
        {
            try
            {
                //  对观测数据进行归类
                foreach (var item in ListMeasure)
                {
                    //  获取目标节点

                    if (item.Count > 0)
                    {
                        string RemoteSatName = null;
                        if (SatIDToSatNameDic.ContainsKey(item[0].link.remoteNodeIndex))
                        {
                            RemoteSatName = SatIDToSatNameDic[item[0].link.remoteNodeIndex];
                        }
                        else
                        {
                            //  否则就直接用Index
                            RemoteSatName = item[0].link.remoteNodeIndex.ToString();
                        }

                        Introduction = "\r\n" + RemoteSatName + "-To-" + SrcName + ":    观测数据:\r\n";

                        //string Introduction = "\r\n" + item[0].link.localNodeType.ToString() + "-" + item[0].link.localNodeIndex + "-To-" + item[0].link.remoteNodeType.ToString() + "-" + item[0].link.remoteNodeIndex + ":    观测数据:";
                        //  依次对观测数据进行保存
                        File.AppendAllText(FileName, Introduction + "\r\n");
                        File.AppendAllText(FileName, Header + "\r\n" + SplitLine);
                        //File.AppendAllText(FileName, SplitLine);
                        File.AppendAllText(FileName, "\r\n");

                        foreach (var Obsitem in item)
                        {
                            if (!WriteFile(Obsitem))
                            {
                                WriteFileState = false;
                                return false;
                            }
                        }
                    }

                }
            }
            catch (Exception)
            {
                WriteFileState = false;
                //throw ex;
            }
            return true;
        }

        #endregion

        #region 写字符串接口定义
        /// <summary>
        /// 初始化观测数据转字符串
        /// </summary>
        /// <param name="_SrcName">源节点名</param>
        /// <param name="FreqNum">频点个数，不大于三，从1开始</param>
        /// <param name="Freq1">频点1</param>
        /// <param name="Freq2">频点2</param>
        /// <param name="Freq3">频点3</param>
        /// <param name="_localNodeType">源节点类型</param>
        /// <param name="_DestName">目标节点名</param>
        /// <param name="_remoteNodeType">目标节点类型</param>
        /// <param name="_Declare">声明</param>
        /// <param name="unit">单位</param>
        /// <returns>True 成功； False 失败</returns>
        //public bool InitCrossLinkDataString(string _SrcName, int FreqNum = 1, double Freq1 = 0.0, double Freq2 = 0.0, double Freq3 = 0.0, string _Declare = "北斗开放实验室", VelPosUnit unit = VelPosUnit.m)
        public bool InitCrossLinkDataString(string _SrcName, double[] Freq, string _Declare = "北斗开放实验室", VelPosUnit unit = VelPosUnit.m)
        {
            switch (Freq.Count())
            {
                case 1:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = OneFreqHeaderKm;
                            break;
                        case VelPosUnit.m:
                            Header = OneFreqHeaderm;
                            break;
                    }
                    SplitLine = OneFreqSplitLine;
                    break;
                case 2:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = TwoFreqHeaderKm;
                            break;
                        case VelPosUnit.m:
                            Header = TwoFreqHeaderm;
                            break;
                    }
                    SplitLine = TwoFreqSplitLine;
                    break;
                case 3:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = ThreeFreqHeaderKm;
                            break;
                        case VelPosUnit.m:
                            Header = ThreeFreqHeaderm;
                            break;
                    }
                    SplitLine = ThreeFreqSplitLine;
                    break;
                //临时添加上下行观测数据格式
                case 0:
                    Header = UpDownObsHeaderm;
                    SplitLine = UpDownObsSplitLine;
                    break;
                default:
                    return false;
            }
            for (int i = 1; i <= Freq.Count(); i++)
            {
                //switch (i)
                //{
                //    case 1:
                //        FreqInfo += "   Freq" + i + "=" + Freq[i].ToString() + "Mhz\t";
                //        break;
                //    case 2:
                //        FreqInfo += "   Freq" + i + "=" + Freq2.ToString() + "Mhz\t";
                //        break;
                //    case 3:
                //        FreqInfo += "   Freq" + i + "=" + Freq3.ToString() + "Mhz\t";
                //        break;
                //    default:

                //        break;
                //}
                FreqInfo += "   Freq" + i + "=" + Freq[i - 1].ToString() + "Mhz\t";
            }

            SrcName = _SrcName;
            //localNodeType = _localNodeType;
            Declare = _Declare;
            Introduction = SrcName + "观测数据" + "\t\t\t\t" + FreqInfo + "\r\n";

            WriteFileState = true;
            return true;
        }

        /// <summary>
        /// 单个观测数据转字符串
        /// </summary>
        /// <param name="measure">观测数据</param>
        /// <returns>True 成功； False 失败</returns>
        string WriteString(List<ObsReceiverOutput> measure)
        {
            string content = null;
            if (WriteFileState)
            {
                try
                {
                    content = measure[0].CurTime.ToString() + "    ";
                    content += measure[0].SatID.ToString().PadLeft(10, ' ') + "    ";
                    for (int i = 0; i < measure.Count; i++)
                    {
                        content += "    " + measure[i].SatObs.A_Pseudo_writein.ToString().PadLeft(19, ' ') + "    " +
                            measure[i].SatObs.A_Carri_writein.ToString().PadLeft(22, ' ') + "    " + measure[i].SatObs.DoplShift.ToString().PadLeft(22, ' ') + "    " +
                            measure[i].SatObs.DotPseudo.ToString().PadLeft(19, ' ');
                    }
                }
                catch (Exception)
                {
                    WriteFileState = false;
                    return null;
                    //throw ex;
                }
            }
            return content;
        }

        /// <summary>
        /// 上下行双向观测数据转字符串
        /// </summary>
        /// <param name="measure">观测数据</param>
        /// <returns>True 成功； False 失败</returns>
        string WriteString(UpDownObsOutPut measure)
        {
            string content = null;
            if (WriteFileState)
            {
                try
                {
                    content = measure.ObsOutPut.CurTime.ToString() + "    ";

                    content += "    " + measure.WN.ToString().PadRight(12, ' ') +
                        "    " + measure.Sow.ToString().PadLeft(15, ' ') +
                        "    " + measure.RecID.ToString().PadLeft(10, ' ') +
                        "    " + measure.SndID.ToString().PadLeft(10, ' ') +
                        "    " + measure.ObsOutPut.SatObs.A_Pseudo_writein.ToString().PadLeft(19, ' ') +
                        "    " + measure.ObsOutPut.SatObs.A_Carri_writein.ToString().PadLeft(22, ' ') +
                        "    " + measure.ObsOutPut.SatObs.DoplShift.ToString().PadLeft(22, ' ') +
                        "    " + measure.ObsOutPut.SatObs.DotPseudo.ToString().PadLeft(19, ' ');
                }
                catch (Exception)
                {
                    WriteFileState = false;
                    return null;
                    //throw ex;
                }
            }
            return content;
        }

        /// <summary>
        /// 观测数据集合转字符串集合，每种观测数据存在一个List中
        /// </summary>
        /// <param name="ListMeasure">观测数据集合</param>
        /// <returns>返回观测数据的字符串集合</returns>
        public List<string> WriteString(List<Dictionary<string, List<ObsReceiverOutput>>> ListMeasure, string remoteNodeName)
        {
            try
            {
                DataBuffer.Add(Declare + "\r\n");
                DataBuffer.Add(Introduction);
                Introduction = remoteNodeName + "-To-" + SrcName + ":    观测数据:";
                DataBuffer.Add("\r\n" + Introduction + "\r\n");
                DataBuffer.Add(Header);
                DataBuffer.Add(SplitLine);
                //  对观测数据进行归类
                foreach (var item in ListMeasure)
                {
                    if (item.Keys.Contains(remoteNodeName))
                    {
                        string content = WriteString(item[remoteNodeName]);
                        if (content == null)
                        {
                            //WriteFileState = false;
                            continue;
                        }
                        DataBuffer.Add(content);
                    }
                }
            }
            catch (Exception)
            {
                WriteFileState = false;
                //throw ex;
            }
            return DataBuffer;
        }

        /// <summary>
        /// 观测数据集合转字符串集合，每种观测数据存在一个List中
        /// </summary>
        /// <param name="ListMeasure">观测数据集合</param>
        /// <returns>返回观测数据的字符串集合</returns>
        public List<string> WriteString(List<Dictionary<string, List<ObsReceiverOutput>>> ListMeasure)
        {
            try
            {
                DataBuffer.Add(Declare + "\r\n");
                DataBuffer.Add(Introduction);
                Introduction = SrcName + ":    观测数据:";
                DataBuffer.Add("\r\n" + Introduction + "\r\n");
                DataBuffer.Add(Header);
                DataBuffer.Add(SplitLine);
                //  对观测数据进行归类
                foreach (var item in ListMeasure)
                {
                    foreach (var item1 in item.Keys)
                    {
                        string content = WriteString(item[item1]);
                        if (content == null)
                        {
                            continue;
                        }
                        DataBuffer.Add(content);
                    }
                    DataBuffer.Add(" ");
                }
            }
            catch (Exception)
            {
                WriteFileState = false;
                //throw ex;
            }
            return DataBuffer;
        }

        /// <summary>
        /// 观测数据集合转字符串集合，每种观测数据存在一个List中
        /// </summary>
        /// <param name="ListMeasure">观测数据集合</param>
        /// <returns>返回观测数据的字符串集合</returns>
        public List<string> WriteString(List<Dictionary<string, UpDownObsOutPut>> ListMeasure, string remoteNodeName)
        {
            try
            {
                DataBuffer.Add(Declare + "\r\n");
                //DataBuffer.Add(Introduction);
                Introduction = remoteNodeName + ": 星地双向观测数据:";
                DataBuffer.Add("\r\n" + Introduction + "\r\n");
                DataBuffer.Add(Header);
                DataBuffer.Add(SplitLine);
                //  对观测数据进行归类
                foreach (var item in ListMeasure)
                {
                    if (item.Keys.Contains(remoteNodeName))
                    {
                        string content = WriteString(item[remoteNodeName]);
                        if (content == null)
                        {
                            continue;
                        }
                        DataBuffer.Add(content);
                    }
                }
            }
            catch (Exception)
            {
                WriteFileState = false;
            }
            return DataBuffer;
        }
        #endregion
    }

    /// <summary>
    /// 写轨迹数据
    /// </summary>
    public class TrajectoryDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 前言
        /// </summary>
        string Introduction = null;

        /// <summary>
        /// 数据说明
        /// </summary>
        string Header = null;
        /// <summary>
        /// 节点名称
        /// </summary>
        private string NodeName = null;

        /// <summary>
        /// 节点类型
        /// </summary>
        private string ModelType = null;

        /// <summary>
        /// 纬经高和XYZ(m)
        /// </summary>
        private const string HeaderBLHXYZm = "        Time (UTC)                B (deg)                   L (deg)                   H (m)                     X (m)                     Y (m)                     Z (m)    ";

        /// <summary>
        /// 纬经高和XYZ(km)
        /// </summary>
        private const string HeaderBLHXYZKm = "        Time (UTC)                B (deg)                   L (deg)                   H (km)                    X (km)                    Y (km)                    Z (km)    ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------    ----------------------    ----------------------    ----------------------    ----------------------    ----------------------    ----------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        /// <summary>
        /// 轨迹数据转字符串缓存
        /// </summary>
        List<string> DataBuffer = new List<string>();
        #endregion

        #region  文件保存接口函数
        /// <summary>
        /// 初始化轨迹数据写文件
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_NodeName">节点名</param>
        /// <param name="_ModelType">节点类型</param>
        /// <param name="coordinateSystem">坐标系统</param>
        /// <param name="unit">单位</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitSatWriteFileCfg(string _FileName, string _NodeName, CoordinateSystem coordinateSystem = CoordinateSystem.J2000, VelPosUnit unit = VelPosUnit.m, string _Declare = "北斗开放实验室")
        {
            FileName = _FileName;
            Declare = _Declare;
            NodeName = _NodeName;
            //ModelType = _ModelType.ToString();

            Header = null;

            switch (unit)
            {
                case VelPosUnit.Km:
                    Header = HeaderBLHXYZKm;
                    break;
                case VelPosUnit.m:
                    Header = HeaderBLHXYZm;
                    break;
            }
            Introduction = ModelType + "-" + NodeName + ": " + "BLH-XYZ" + "-" + coordinateSystem.ToString() + " 轨迹";

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();
                WriteFileState = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                WriteFileState = false;
                return false;
            }

            return true;

        }

        /// <summary>
        /// 轨迹数据写文件
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="X1">B</param>
        /// <param name="X2">L</param>
        /// <param name="X3">H</param>
        /// <param name="X4">X</param>
        /// <param name="X5">Y</param>
        /// <param name="X6">Z</param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, double X1, double X2, double X3, double X4, double X5, double X6)
        {
            if (WriteFileState)
            {
                try
                {
                    UTCToStringClass utcstr = new UTCToStringClass();
                    string UTCStr = utcstr.NewUTCToString(UTC);
                    string content = UTCStr + "    " + X1.ToString().PadLeft(22, ' ') +
                        "    " + X2.ToString().PadLeft(22, ' ') +
                        "    " + X3.ToString().PadLeft(22, ' ') +
                        "    " + X4.ToString().PadLeft(22, ' ') +
                        "    " + X5.ToString().PadLeft(22, ' ') +
                        "    " + X6.ToString().PadLeft(22, ' ') +
                        "\r\n";

                    File.AppendAllText(FileName, content);
                }
                catch (Exception ex)
                {
                    WriteFileState = false;
                    throw ex;
                }

            }
            return true;
        }

        /// <summary>
        /// 以BLH-XYZ形式写轨迹数据文件
        /// </summary>
        /// <param name="UserPPosition">定位信息</param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(List<UserTrajectoryClass> UserTrajectoryData)
        {
            if (WriteFileState)
            {
                foreach (var item in UserTrajectoryData)
                {
                    WriteFile(item.UserPosBLH.epoch, item.UserPosBLH.B, item.UserPosBLH.L, item.UserPosBLH.H,
                        item.UserPosXYZ.X, item.UserPosXYZ.Y, item.UserPosXYZ.Z);
                }

                return true;
            }
            else
            {
                return false;
            }

        }

        #endregion

        #region//返回字符串接口

        /// <summary>
        ///  初始化轨迹数据转字符串
        /// </summary>
        /// <param name="_NodeName">节点名</param>
        /// <param name="_ModelType">节点类型</param>
        /// <param name="_BLHXYZEnum">坐标类型</param>
        /// <param name="coordinateSystem">坐标系统</param>
        /// <param name="unit">单位</param>
        /// <param name="_Declare">声明</param>
        public void InitTrajectoryDataWriter(string _NodeName, BLHXYZEnum _BLHXYZEnum = BLHXYZEnum.XYZ,
            CoordinateSystem coordinateSystem = CoordinateSystem.CGCS2000, VelPosUnit unit = VelPosUnit.m, string _Declare = "北斗开放实验室")
        {
            Declare = _Declare;
            NodeName = _NodeName;
            //ModelType = _ModelType.ToString();

            Header = null;
            switch (unit)
            {
                case VelPosUnit.Km:
                    Header = HeaderBLHXYZm;
                    break;
                case VelPosUnit.m:
                    Header = HeaderBLHXYZm;
                    break;
            }
            //switch (_BLHXYZEnum)
            //{
            //    case BLHXYZEnum.BLH:
            //        switch (unit)
            //        {
            //            case VelPosUnit.Km:
            //                Header = HeaderBLHXYZm;
            //                break;
            //            case VelPosUnit.m:
            //                Header = HeaderBLHXYZm;
            //                break;
            //        }
            //        break;
            //    case BLHXYZEnum.XYZ:
            //        switch (unit)
            //        {
            //            case VelPosUnit.Km:
            //                Header = HeaderBLHXYZm;
            //                break;
            //            case VelPosUnit.m:
            //                Header = HeaderBLHXYZm;
            //                break;
            //        }
            //        break;
            //    default:
            //        break;
            //}
            Introduction = ModelType + "-" + NodeName + ": " + "用户轨迹-" + coordinateSystem.ToString();
            WriteFileState = true;
        }

        /// <summary>
        /// 轨迹数据转字符串
        /// </summary>
        /// <param name="UTC"></param>
        /// <param name="X1"></param>
        /// <param name="X2"></param>
        /// <param name="X3"></param>
        /// <param name="X4"></param>
        /// <param name="X5"></param>
        /// <param name="X6"></param>
        /// <returns></returns>
        string WriteString(UTCTime UTC, double X1, double X2, double X3, double X4, double X5, double X6)
        {
            string content = null;
            if (WriteFileState)
            {
                UTCToStringClass utcstr = new UTCToStringClass();
                string UTCStr = utcstr.NewUTCToString(UTC);
                content = UTCStr + "    " + X1.ToString().PadLeft(22, ' ') +
                        "    " + X2.ToString().PadLeft(22, ' ') +
                        "    " + X3.ToString().PadLeft(22, ' ') +
                        "    " + X4.ToString().PadLeft(22, ' ') +
                        "    " + X5.ToString().PadLeft(22, ' ') +
                        "    " + X6.ToString().PadLeft(22, ' ') +
                        "\r\n";
            }
            return content;
        }

        /// <summary>
        /// 轨迹数据转字符串
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="blh">纬经高</param>
        /// <param name="xyz">xyz</param>
        /// <returns>True 成功； False 失败</returns>
        public string WriteString(UTCTime UTC, BLH_PosVel blh, NodePosVel xyz)
        {
            string content = null;
            UTCToStringClass utcstr = new UTCToStringClass();
            string UTCStr = utcstr.NewUTCToString(UTC);
            content = UTCStr +
                "    " + blh.B.ToString().PadLeft(22, ' ') +
                "    " + blh.L.ToString().PadLeft(22, ' ') +
                "    " + blh.H.ToString().PadLeft(22, ' ') +
                "    " + xyz.X.ToString().PadLeft(22, ' ') +
                "    " + xyz.Y.ToString().PadLeft(22, ' ') +
                "    " + xyz.Z.ToString().PadLeft(22, ' ');
            return content;
        }

        /// <summary>
        ///  轨迹数据集合转字符串集合，传进来的可以使XYZ或者是BLH
        /// </summary>
        /// <param name="UserTrajectory">轨迹数据集合</param>
        /// <returns>转换结果</returns>
        public List<string> WriteToStringList(List<UserTrajectoryClass> UserTrajectory)
        {
            if (WriteFileState)
            {
                DataBuffer.Add(Declare);
                DataBuffer.Add("");
                DataBuffer.Add(Introduction);
                DataBuffer.Add("");
                DataBuffer.Add(Header);
                DataBuffer.Add(SplitLine);
                for (int i = 0; i < UserTrajectory.Count; i++)
                {
                    DataBuffer.Add(WriteString(UserTrajectory[i].UserPosBLH.epoch, UserTrajectory[i].UserPosBLH, UserTrajectory[i].UserPosXYZ));
                }
            }
            return DataBuffer;
        }
        #endregion
    }

    /// <summary>
    /// 写定位数据
    /// </summary>
    public class PositionDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 前言
        /// </summary>
        string Introduction = null;

        /// <summary>
        /// 节点名称
        /// </summary>
        private string NodeName = null;

        /// <summary>
        /// 节点类型
        /// </summary>
        private string ModelType = null;

        /// <summary>
        /// 数据说明XYZ(Km)
        /// </summary>
        private const string HeaderXYZKm = "        Time (UTC)                 X (km)                    Y (km)                     Z (km)                  δX (km)                  δY (km)                  δZ (km)    ";

        /// <summary>
        ///  数据说明XYZ(m)
        /// </summary>
        private const string HeaderXYZm = "        Time (UTC)                 X (m)                      Y (m)                     Z (m)                   δX (m)                   δY (m)                  δZ (m)       ";

        /// <summary>
        /// 数据说明BLH(Km)
        /// </summary>
        private const string HeaderBLHKm = "        Time (UTC)                 B (m)                      L (m)                     H (km)                   δB (°)                   δL (°)                  δH (km)       ";

        /// <summary>
        /// 数据说明BLH(m)
        /// </summary>
        private const string HeaderBLHm = "        Time (UTC)                 B (m)                      L (m)                     H (m)                   δB (°)                   δL (°)                  δH (m)       ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------    ----------------------    ----------------------    ----------------------    ----------------------    ----------------------    ----------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        /// <summary>
        /// 定位数据转字符串缓存
        /// </summary>
        List<string> DataBuffer = new List<string>();
        #endregion

        #region  文件保存接口函数
        /// <summary>
        /// 初始化写定位数据文件
        /// </summary>
        /// <param name="_FileName">文件名</param>
        /// <param name="_NodeName">节点名</param>
        /// <param name="_ModelType">节点类型</param>
        /// <param name="_BLHXYZEnum">坐标类型</param>
        /// <param name="coordinateSystem">坐标系统</param>
        /// <param name="unit">单位</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitSatWriteFileCfg(string _FileName, string _NodeName, BLHXYZEnum _BLHXYZEnum = BLHXYZEnum.XYZ, CoordinateSystem coordinateSystem = CoordinateSystem.J2000, VelPosUnit unit = VelPosUnit.m, string _Declare = "北斗开放实验室")
        {
            FileName = _FileName;
            Declare = _Declare;
            NodeName = _NodeName;
            //ModelType = _ModelType.ToString();

            string Header = null;
            switch (_BLHXYZEnum)
            {
                case BLHXYZEnum.BLH:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = HeaderBLHKm;
                            break;
                        case VelPosUnit.m:
                            Header = HeaderBLHm;
                            break;
                    }
                    break;
                case BLHXYZEnum.XYZ:
                    switch (unit)
                    {
                        case VelPosUnit.Km:
                            Header = HeaderXYZKm;
                            break;
                        case VelPosUnit.m:
                            Header = HeaderXYZm;
                            break;
                    }
                    break;
                default:
                    break;
            }
            Introduction = ModelType + "-" + NodeName + ": " + _BLHXYZEnum.ToString() + "-" + coordinateSystem.ToString() + " 定位误差";

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();
                WriteFileState = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                WriteFileState = false;
                return false;
            }

            return true;

        }

        /// <summary>
        /// 写单个定位数据
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="X"></param>
        /// <param name="Y"></param>
        /// <param name="Z"></param>
        /// <param name="Detax"></param>
        /// <param name="Detaxy"></param>
        /// <param name="Detaxz"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, double X, double Y, double Z, double Detax, double Detaxy, double Detaxz)
        {
            if (WriteFileState)
            {
                try
                {
                    UTCToStringClass utcstr = new UTCToStringClass();
                    string UTCStr = utcstr.NewUTCToString(UTC);
                    string content = UTCStr + "    " + X.ToString().PadLeft(22, ' ') +
                        "    " + Y.ToString().PadLeft(22, ' ') +
                        "    " + Z.ToString().PadLeft(22, ' ') +
                        "    " + Detax.ToString().PadLeft(22, ' ') +
                        "    " + Detaxy.ToString().PadLeft(22, ' ') +
                        "    " + Detaxz.ToString().PadLeft(22, ' ') + "\r\n";

                    File.AppendAllText(FileName, content);
                }
                catch (Exception ex)
                {
                    WriteFileState = false;
                    throw ex;
                }

            }
            return true;
        }

        /// <summary>
        /// 写单个定位数据
        /// </summary>
        /// <param name="UTC"></param>
        /// <param name="NodePosVel"></param>
        /// <param name="NodePosVelErr"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, NodePosVel NodePosVel, NodePosVel NodePosVelErr)
        {
            if (WriteFileState)
            {
                if (!WriteFile(UTC, NodePosVel.X, NodePosVel.Y, NodePosVel.Z, NodePosVelErr.X,
                    NodePosVelErr.Y, NodePosVelErr.Z))
                {
                    WriteFileState = false;
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 写单个定位数据
        /// </summary>
        /// <param name="UTC"></param>
        /// <param name="NodePosVel"></param>
        /// <param name="NodePosVelErr"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, BLH_PosVel NodePosVel, BLH_PosVel NodePosVelErr)
        {
            if (WriteFileState)
            {
                if (!WriteFile(UTC, NodePosVel.B, NodePosVel.L, NodePosVel.H, NodePosVelErr.B,
                    NodePosVelErr.L, NodePosVelErr.H))
                {
                    WriteFileState = false;
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 写单个定位数据
        /// </summary>
        /// <param name="PosErr"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(PosPosErrClass PosErr)
        {
            if (WriteFileState)
            {
                if (!WriteFile(PosErr.UTC, PosErr.Pos[0], PosErr.Pos[1], PosErr.Pos[2],
                    PosErr.PosErr[0], PosErr.PosErr[1], PosErr.PosErr[2]))
                {
                    WriteFileState = false;
                    return false;
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 写定位数据集合，以X、Y、Z形式保存
        /// </summary>
        /// <param name="UserPPosition">定位数据集合</param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(List<UserPPositionData> UserPPosition)
        {
            if (WriteFileState)
            {
                foreach (var item in UserPPosition)
                {
                    WriteFile(item.Time, item.PPosition_XYZ, item.Error_XYZ);
                }
                return true;
            }
            else
            {
                return false;
            }

        }
        #endregion
    }

    public class DopEvaluationDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
       // private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        //private string Declare = "北斗开放实验室";

        /// <summary>
        /// 前言
        /// </summary>
        //string Introduction = null;

        /// <summary>
        /// 节点名称
        /// </summary>
        //private string NodeName = null;

        /// <summary>
        /// 节点类型
        /// </summary>
        //private string ModelType = null;

        /// <summary>
        /// 数据说明XYZ(Km)
        /// </summary>
        private const string HeaderXYZKm = "        Time (UTC)              MinValue(m)                MaxValue(m)              MeanValue(m)               Variance        ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------    ----------------------    ----------------------    ----------------------    ----------------------";

        #endregion

        public void WriteData(string filePath)
        {

        }
    }

    /// <summary>
    /// UTC转字符串
    /// </summary>
    public class UTCToStringClass
    {
        /// <summary>
        /// UTC转字符串
        /// </summary>
        /// <param name="UTC"></param>
        /// <returns>转换结果</returns>
        public string NewUTCToString(UTCTime UTC)
        {
            string UTCStr = null;

            UTCStr = UTC.Year.ToString().PadLeft(4, ' ') + "-" + UTC.Mon.ToString().PadLeft(2, '0') + "-" +
                        UTC.Day.ToString().PadLeft(2, '0') + " " + UTC.Hour.ToString().PadLeft(2, '0') + ":" +
                        UTC.Min.ToString().PadLeft(2, '0') + ":";

            UTCStr += UTC.Sec.ToString("00.000");
            return UTCStr;
        }
    }

    /// <summary>
    /// 钟差拟合数据(Add by lhz)
    /// </summary>
    public class ClockFitDataWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 前言
        /// </summary>
        string Introduction = null;
        /// <summary>
        /// 对象名称
        /// </summary>
        private string NodeName = null;

        /// <summary>
        /// 数据说明（m)
        /// </summary>
        private const string Header = "       Time (UTC)                 ClkErr_a0                  ClkErr_a1                    ClkErr_a2  ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------      --------------------       --------------------         --------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        /// <summary>
        /// 钟差数据转字符串缓存
        /// </summary>
        List<string> DataBuffer = new List<string>();
        #endregion

        #region 接口定义
        /// <summary>
        /// 写钟差文件初始化
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_NodeName">节点名</param>
        /// <param name="NodeType">节点类型</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitClockFitDataFile(string _FileName, string _NodeName, string _Declare = "北斗开放实验室")
        {

            FileName = _FileName;
            NodeName = _NodeName;
            Declare = _Declare;

            //string Introduction = NodeType.ToString() + "-" + NodeName + ":钟差数据";
            string Introduction = NodeName + ":钟差拟合数据";
            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();
            }
            catch (Exception)
            {
                WriteFileState = false;
                return false;
            }

            WriteFileState = true;
            return true;
        }


        /// <summary>
        /// 单个钟差数据写文件
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="ClkErr">钟差</param>
        /// <returns>True 成功； False 失败</returns>        
        public bool WriteFile(UTCTime UTC, double ClkErr_a0, double ClkErr_a1, double ClkErr_a2)
        {
            if (WriteFileState)
            {
                try
                {
                    UTCToStringClass utcstr = new UTCToStringClass();
                    string content = utcstr.NewUTCToString(UTC) + "    " + ClkErr_a0.ToString().PadLeft(20, ' ') + ClkErr_a1.ToString().PadLeft(20, ' ') + ClkErr_a2.ToString().PadLeft(20, ' ') + "\r\n";
                    File.AppendAllText(FileName, content);
                }
                catch (Exception)
                {
                    WriteFileState = false;
                }
            }
            return true;
        }

        /// <summary>
        /// 钟差数据集合写文件
        /// </summary>
        /// <param name="ClkDataList">钟差数据集合</param>
        /// <returns>True 成功； False 失败</returns>  
        public bool WriteFile(List<ClkErrFitMsg> ClockErrDic)
        {
            if (WriteFileState)
            {
                foreach (var item in ClockErrDic)
                {
                    WriteFile(item.FitTime, item.a0, item.a1, item.a2);
                }
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region//返回字符串接口
        /// <summary>
        /// 钟差转字符串初始化
        /// </summary>
        /// <param name="_NodeName">节点名</param>
        /// <param name="NodeType">节点类型</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public void InitClockFitDataFile(string _NodeName, string _Declare = "北斗开放实验室")
        {
            NodeName = _NodeName;
            Declare = _Declare;
            //Introduction = NodeType.ToString() + "-" + NodeName + "-钟差数据";
            Introduction = NodeName + "-钟差拟合数据";
            WriteFileState = true;
        }

        /// <summary>
        /// 单个钟差数据转字符串
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="ClkErr">钟差</param>
        /// <returns>True 成功； False 失败</returns>  
        public string WriteString(UTCTime UTC, double ClkErr_a0, double ClkErr_a1, double ClkErr_a2)
        {
            string content = null;
            if (WriteFileState)
            {
                UTCToStringClass utcstr = new UTCToStringClass();
                content = utcstr.NewUTCToString(UTC) + "    " + ClkErr_a0.ToString().PadLeft(20, ' ') + "    " + ClkErr_a1.ToString().PadLeft(25, ' ') + "    " + ClkErr_a2.ToString().PadLeft(25, ' ') + "    ";
            }
            return content;
        }

        /// <summary>
        /// 钟差数据集合转字符串集合
        /// </summary>
        /// <param name="UTC">历元</param>
        /// <param name="ClkErr">钟差</param>
        /// <returns>True 成功； False 失败</returns>  
        public List<string> WriteToStringList(List<ClkErrFitMsg> dataList)
        {
            DataBuffer.Add(Declare);
            DataBuffer.Add("");
            DataBuffer.Add(Introduction);
            DataBuffer.Add("");
            DataBuffer.Add(Header);
            DataBuffer.Add(SplitLine);
            for (int i = 0; i < dataList.Count; i++)
            {
                DataBuffer.Add(WriteString(dataList[i].FitTime, dataList[i].a0, dataList[i].a1, dataList[i].a2));
            }
            return DataBuffer;
        }
        #endregion
    }

    /// <summary>
    /// 电离层VTEC写文件(add by liuy on 2017 7 18)
    /// </summary>
    public class IonVTECWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 电离层八参数
        /// </summary>
        private const string Header = "       Time (UTC)          B（纬度）     L（经度）           VTEC         ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------  ------------  ------------  ---------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        #endregion

        #region 接口定义
        /// <summary>
        /// 电离层八参写文件初始化
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitIonVTECDataFile(string _FileName, string _Declare = "北斗开放实验室")
        {
            FileName = _FileName;
            Declare = _Declare;

            string Introduction = "电离层VTEC";

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();

                WriteFileState = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                WriteFileState = false;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 单个电离层VTEC写文件
        /// </summary>
        /// <param name="UTC"></param>
        /// <param name="B"></param>
        /// <param name="L"></param>
        /// <param name="VTEC"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, double B, double L, double VTEC)
        {
            if (WriteFileState)
            {
                UTCToStringClass utcstr = new UTCToStringClass();
                string Content = utcstr.NewUTCToString(UTC);
                Content += "  " + B.ToString().PadLeft(12, ' ');
                Content += "  " + L.ToString().PadLeft(12, ' ');
                Content += "  " + VTEC.ToString().PadLeft(21, ' ') + "\r\n";

                try
                {
                    File.AppendAllText(FileName, Content);
                }
                catch (Exception)
                {
                    WriteFileState = false;
                    //throw ex;
                }
            }
            return true;
        }
        #endregion

    }

    /// <summary>
    /// 气象参数写文件(add by liuy on 2017 7 22)
    /// </summary>
    public class ClimateWriter
    {
        #region 数据定义
        /// <summary>
        /// 文件名
        /// </summary>
        private string FileName = null;

        /// <summary>
        /// 声明，默认为“北斗开放实验室”
        /// </summary>
        private string Declare = null;

        /// <summary>
        /// 电离层八参数
        /// </summary>
        private const string Header = "       Time (UTC)          B（纬度）     H（高度）         P（气压）              P（温度）              RH（湿度%）    ";

        /// <summary>
        /// 分隔符
        /// </summary>
        private const string SplitLine = "-----------------------  ------------  ------------  ---------------------  ---------------------  ---------------------";

        /// <summary>
        /// 用于标记文件写入的状态,写文件配置未初始化时，不进行任何写文件操作
        /// </summary>
        private bool WriteFileState = false;

        #endregion

        #region 接口定义
        /// <summary>
        /// 电离层八参写文件初始化
        /// </summary>
        /// <param name="_FileName">目标文件名</param>
        /// <param name="_Declare">声明</param>
        /// <returns>True 成功； False 失败</returns>
        public bool InitClimateDataFile(string _FileName, string _Declare = "北斗开放实验室")
        {
            FileName = _FileName;
            Declare = _Declare;

            string Introduction = "气象参数";

            try
            {
                StreamWriter sw = new StreamWriter(FileName);
                sw.WriteLine(Declare + "\r\n");
                sw.WriteLine(Introduction + "\r\n");
                sw.WriteLine(Header);
                sw.WriteLine(SplitLine);
                sw.Close();

                WriteFileState = true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                WriteFileState = false;
                return false;
            }
            return true;
        }

        /// <summary>
        /// 单个电离层VTEC写文件
        /// </summary>
        /// <param name="UTC"></param>
        /// <param name="B"></param>
        /// <param name="L"></param>
        /// <param name="VTEC"></param>
        /// <returns>True 成功； False 失败</returns>
        public bool WriteFile(UTCTime UTC, double B, double H, double P, double T, double RH)
        {
            if (WriteFileState)
            {
                UTCToStringClass utcstr = new UTCToStringClass();
                string Content = utcstr.NewUTCToString(UTC);
                Content += "  " + B.ToString().PadLeft(12, ' ');
                Content += "  " + H.ToString().PadLeft(12, ' ');
                Content += "  " + P.ToString().PadLeft(21, ' ');
                Content += "  " + T.ToString().PadLeft(21, ' ');
                Content += "  " + RH.ToString().PadLeft(21, ' ') + "\r\n";

                try
                {
                    File.AppendAllText(FileName, Content);
                }
                catch (Exception)
                {
                    WriteFileState = false;
                    //throw ex;
                }
            }
            return true;
        }
        #endregion

    }

    #endregion
}
